//
//  SearchHistoryViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/2/26.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit

class SearchHistoryViewController: UIViewController {

    fileprivate var historyView : UIScrollView!
    var  backCompletion: ((NSString) -> Swift.Void)? = nil
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        
        // history
        historyView = UIScrollView.init(frame: CGRect.init(x: 0, y: self.navgationBarBottom, width: MAINSCREEN_WIDTH, height: MAINSCREEN_HEIGHT-self.navgationBarBottom))
        historyView.contentSize = CGSize.init(width: historyView.width, height: historyView.height)
        historyView.backgroundColor = self.view.backgroundColor
        self.view.addSubview(historyView)
        
        let hisArray = GetUserDefaults(key: SearchText_Keyword)
        if hisArray.isKind(of: NSArray.classForCoder()) && hisArray.count>0 {
            self.layoutSearchAction(bgView: historyView, title: "历史记录", listArray: hisArray as! NSArray)
        }else{
            self.layoutSearchAction(bgView: historyView, title: "暂无历史记录", listArray: NSArray.init())
        }
    }
    
    func layoutSearchAction(bgView: UIScrollView, title: NSString, listArray: NSArray) {
        
        let tipLabel = UILabel.init(frame: CGRect.init(x: 12, y: 12, width: 100, height: 20))
        tipLabel.font = UIFont.systemFont(ofSize: 15)
        tipLabel.text = title as String
        bgView.addSubview(tipLabel)
        
        if listArray.count > 0 {
            let deletBtn = UIButton.init(frame: CGRect.init(x: bgView.width-12-20, y: 12, width: 20, height: 20))
            deletBtn.setImage(UIImage.init(named: "search_Delet"), for: .normal)
            deletBtn.addTarget(self, action: #selector(clearDataAction(sender:)), for: .touchUpInside)
            bgView.addSubview(deletBtn)
        }
        
        var zX = CGFloat(12) //记录按钮的X
        var zY = CGFloat(36) //记录按钮的Y
        let zH = CGFloat(30) //记录按钮的高
        for (index,value) in listArray.enumerated() {
            
            if let valueC : String = value as? String {
                
                let attrDic = [NSAttributedStringKey.font:UIFont.systemFont(ofSize: 15)]
                var size = SizeWithFont(content: valueC as NSString, attrDic: attrDic as NSDictionary, maxSize: CGSize.init(width: 1000, height: 18))
                size.width = size.width + 18 //每个按钮的长度
                //如果摆放的按钮大于屏幕宽 则另起一行
                if (zX+size.width+12) > bgView.width {
                    zY = zY + (zH+10) //10是按钮上下间的间隔
                    zX = 12 //初始值
                }
                //最后一个按钮离底部距离
                if (zY+zH)>bgView.contentSize.height {
                    //加一个上下间隔和按钮高度
                    bgView.contentSize.height = bgView.contentSize.height + (10+zH)
                }
                
                let buton = UIButton.init(type: .custom)
                buton.tag = index
                buton.frame = CGRect.init(x: zX, y: zY, width: size.width, height: zH)
                buton.backgroundColor = UIColor.white
                buton.setTitle(valueC as String, for: .normal)
                buton.setTitleColor(UIColorFromRGB(rgbValue: 0x606060), for: .normal)
                buton.titleLabel?.font = UIFont.systemFont(ofSize: 15)
                buton.layer.cornerRadius = 9
                buton.layer.masksToBounds = true
                buton.layer.borderWidth = 1
                buton.layer.borderColor = UIColor.defaultLineColor.cgColor
                buton.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside)
                bgView.addSubview(buton)
                
                zX = zX + (buton.width+12) //12是按钮左右间的间隔
            }
        }
    }
    
    @objc func clearDataAction(sender: UIButton) {
        let hisArray = GetUserDefaults(key: SearchText_Keyword)
        for value in (hisArray as! NSArray).enumerated() {
            if let btn = historyView.viewWithTag(value.offset) {
                btn.removeFromSuperview()
            }
        }
        RemoveUserDefaults(key: SearchText_Keyword)
        self.view.makeToast("删除本地记录完成", duration: 2.0, position: .center)
    }
    
    @objc func buttonAction(sender: UIButton) {
        if backCompletion != nil {
            backCompletion?(sender.currentTitle! as NSString)
        }
        self.navigationController?.popViewController(animated: true)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
